package com.xuchen.zhenyang.core.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义线程池
 */
//@Configuration
//@EnableAsync
public class ThreadPoolBean {

    @Bean("myThreadPool")
    public Executor createTaskThreadPool(){
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(10);
        threadPool.setMaxPoolSize(200);
        threadPool.setWaitForTasksToCompleteOnShutdown(true);
        threadPool.setKeepAliveSeconds(60);
        threadPool.setAwaitTerminationSeconds(60);
        threadPool.setQueueCapacity(Integer.MIN_VALUE);
        threadPool.setThreadNamePrefix("MyTaskPool-");
        // CallerRunsPolicy：不在新线程中执行任务，而是由调用者所在的线程来执行 如果执行器已关闭,则丢弃.
        threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPool.initialize();
        return threadPool;
    }
}
